home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_pcdp / ada / pcbs.ada < prev    next >
Text File  |  1996-01-30  |  2KB  |  79 lines

  1. with Text_IO; use Text_IO;
  2. with Semaphore_Package; use Semaphore_Package;
  3. procedure PCBS is
  4.  
  5.   N: constant Integer := 10;
  6.   B: array(0..N-1) of Integer;
  7.   In_Ptr, Out_Ptr: Integer := 0;
  8.   Count: Integer := 0;
  9.  
  10.   S: Binary_Semaphore := Init(1);
  11.   Not_Empty, Not_Full: Binary_Semaphore := Init(0);
  12.  
  13.   task Producer is
  14.     pragma Priority(10);
  15.   end Producer;
  16.   task Consumer1 is
  17.     pragma Priority(7);
  18.   end Consumer1;
  19.   task Consumer2 is
  20.     pragma Priority(7);
  21.   end Consumer2;
  22.  
  23.   task body Producer is
  24.     I: Integer := 0;
  25.     Local: Integer := 0;
  26.   begin
  27.     loop
  28.       I := I + 1;
  29.       Put_Line("Produce  " & Integer'Image(I));
  30.       if I mod 40 = 0 then delay 1.0; end if;
  31.       if Local = N then Wait(Not_Full); end if;
  32.       B(In_Ptr) := I;
  33.       Wait(S);
  34.       Count := Count + 1;
  35.       Local := Count;
  36.       Signal(S);
  37.       if Local = 1 then Signal(Not_Empty); end if;
  38.       In_Ptr := (In_Ptr + 1) mod N;
  39.     end loop;
  40.   end Producer;
  41.  
  42.   task body Consumer1 is
  43.     I: Integer;
  44.     Local: Integer := 0;
  45.   begin
  46.     loop
  47.       if Local = 0 then Wait(Not_Empty); end if;
  48.       I := B(Out_Ptr);
  49.       Wait(S);
  50.       Count := Count - 1;
  51.       Local := Count;
  52.       Signal(S);
  53.       if Local = N-1 then Signal(Not_Full); end if;
  54.       Out_Ptr := (Out_Ptr + 1) mod N;
  55.       Put_Line("Consume 1 " & Integer'Image(I));
  56.     end loop;
  57.   end Consumer1;
  58.  
  59.   task body Consumer2 is
  60.     I: Integer;
  61.     Local: Integer := 0;
  62.   begin
  63.     loop
  64.       if Local = 0 then Wait(Not_Empty); end if;
  65.       I := B(Out_Ptr);
  66.       Wait(S);
  67.       Count := Count - 1;
  68.       Local := Count;
  69.       Signal(S);
  70.       if Local = N-1 then Signal(Not_Full); end if;
  71.       Out_Ptr := (Out_Ptr + 1) mod N;
  72.       Put_Line("Consume 2 " & Integer'Image(I));
  73.     end loop;
  74.   end Consumer2;
  75.  
  76. begin
  77.   null;
  78. end PCBS;
  79.